package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    // --1--  新增地址  请求url地址：/addressBook
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        log.info("[>>>--<地址模块>新增地址]: addressBook={}",addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("保存地址成功");
    }

    // --2--  设置默认地址  请求url地址：/addressBook/default  "id": "1417414926166769666"
    @PutMapping("/default")
    public R<String> mydefault(@RequestBody AddressBook addressBook){
        log.info("[>>>--<地址模块>设置默认地址]: addressBook.id={}",addressBook.getId());
        //-1-- 当前用户 所有地址is_default更新为0
        addressBookService.update(
                new LambdaUpdateWrapper<AddressBook>()
                        .eq(AddressBook::getUserId,BaseContext.getCurrentId()).set(AddressBook::getIsDefault,0)
        );

        //-2-- 当前用户 当前地址is_default更新为1
        addressBookService.update(
                new LambdaUpdateWrapper<AddressBook>()
                        .eq(AddressBook::getId,addressBook.getId()).set(AddressBook::getIsDefault,1)
        );
        return R.success("设置成功");
    }


    // --3--   根据ID查询地址 请求url地址：/addressBook/1417414526093082626
    //addressBook.id为主键唯一
    @GetMapping("/{id}")
    public R<AddressBook> addressBookById(@PathVariable Long id){
        log.info("[>>>--<地址模块>id查找地址]: id={}",id);
        return R.success(
                addressBookService.getOne(
                        new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getId,id)));
    }


    // --4--  查询默认地址     请求url地址：/addressBook/default
    @GetMapping("/default")
    public R<AddressBook> seleteMydefault(){
        log.info("[>>>--<地址模块>查找当前用户默认地址]: ---------------");
        return R.success(
                addressBookService.getOne(
                        new LambdaQueryWrapper<AddressBook>()
                                .eq(AddressBook::getUserId,BaseContext.getCurrentId())
                                .eq(AddressBook::getIsDefault,1)));

    }

    // --5--   查询指定用户的全部地址   请求url地址：/addressBook/list
    @GetMapping("/list")
    public R<List<AddressBook>> addressBookList(){
        log.info("[>>>--<地址模块>展示所有地址]: ===========================");
        //优化：默认地址排前面,新修改的优先
        return R.success(addressBookService.list(
                new LambdaQueryWrapper<AddressBook>()
                        .eq(AddressBook::getUserId,BaseContext.getCurrentId())
                        .orderByDesc(AddressBook::getIsDefault)
                        .orderByDesc(AddressBook::getUpdateTime)));
    }

    // --6--   删除地址【可能是多个】
    // 请求url地址：/addressBook?ids=1417414526093082626,141741452609308230
    @DeleteMapping
    public R<String> myDelete(@RequestParam List<Long> ids){
        log.info("[>>>--<地址模块>删除地址]: ids={}",ids);
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    // --7--   修改地址  请求url地址：/addressBook
    @PutMapping
    public R myUpdata(@RequestBody AddressBook addressBook){
        log.info("[>>>--<地址模块>修改地址]: addressBook={}",addressBook);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }




























}
